package com.salton123.whiteboard.model.bean;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.Log;

/**
 * User: wujinsheng1@yy.com
 * Date: 2020/5/21 12:26
 * ModifyTime: 12:26
 * Description:
 */
public class SvgPath extends Path {
    private String SVG_HEADER =
            "<svg width=\"100%\" height=\"100%\" " +
                    " xmlns=\"http://www.w3.org/2000/svg\"" +
                    "viewBox=\"0 0 0 0\"" +
                    " version=\"1.1\">";
    private String SVG_FOOTER = "</svg>";
    private String SVG_CONTENT = "<path d=\"%s\" fill=\"%s\" stroke=\"%s\" stroke-width=\"%s\" />";
    private StringBuffer mPathBuffer = new StringBuffer();
    private StringBuffer mSvgInfoBuilder = new StringBuffer();

    private String strokeColor = "#ff0000";
    private String strokeWidth = "3";
    private String fillColor = "#00FF00";

    public void setStrokeColor(String strokeColor) {
        this.strokeColor = strokeColor;
    }

    public void setStrokeWidth(String strokeWidth) {
        this.strokeWidth = strokeWidth;
    }

    public void setFillColor(String fillColor) {
        this.fillColor = fillColor;
    }

    private static final String TAG = "SvgPath";

    public synchronized String build() {
        mSvgInfoBuilder.append(SVG_HEADER);
        mPathBuffer.append("z");
        Log.i(TAG, "" + mPathBuffer.toString());
        mSvgInfoBuilder.append(String.format(SVG_CONTENT, mPathBuffer.toString(),
                fillColor, strokeColor, strokeWidth));
        mSvgInfoBuilder.append(SVG_FOOTER);
        return mSvgInfoBuilder.toString();
    }

    public void draw(Canvas canvas, Paint paint) {
        canvas.drawPath(this, paint);
    }

    @Override
    public void reset() {
        super.reset();
        mPathBuffer = new StringBuffer();
    }

    @Override
    public void lineTo(float x, float y) {
        mPathBuffer.append("L" + x + "," + y + " ");
        super.lineTo(x, y);
    }

    @Override
    public void rLineTo(float dx, float dy) {
        mPathBuffer.append("l" + dx + "," + dy + " ");
        super.rLineTo(dx, dy);
    }

    @Override
    public void moveTo(float x, float y) {
        mPathBuffer.append("M" + x + "," + y + " ");
        super.moveTo(x, y);
    }

    @Override
    public void rMoveTo(float dx, float dy) {
        mPathBuffer.append("m" + dx + "," + dy + " ");
        super.rMoveTo(dx, dy);
    }

    @Override
    public void quadTo(float x1, float y1, float x2, float y2) {
        mPathBuffer.append("Q" + x1 + "," + y1 + " " + x2 + "," + y2 + " ");
        super.quadTo(x1, y1, x2, y2);
    }

    @Override
    public void rQuadTo(float dx1, float dy1, float dx2, float dy2) {
        mPathBuffer.append("q" + dx1 + "," + dy1 + " " + dx2 + "," + dy2 + " ");
        super.rQuadTo(dx1, dy1, dx2, dy2);
    }

    @Override
    public void addOval(RectF oval, Direction dir) {
        super.addOval(oval, dir);

    }
}
